package org.kitten.core.util;

import java.util.*;

/**
 * faked map which behaves like a Collection
 * @author sb.yin(uniquejava@gmail.com)
 * @version 1.0
 * @since 2010-7-24
 */
public class MyListMap implements Collection<Object[]> {
	private List list = new ArrayList();
	private Map map = new HashMap();

	public void put(Object key, Object value) {
		if (map.containsKey(key)) {
			list.remove(((Integer) map.get(key)).intValue());
		}
		list.add(new Object[] { key, value });
		map.put(key, new Integer(list.size()));
	}

	public boolean add(Object[] e) {
		put(e[0], e[1]);
		return false;
	}

	public boolean addAll(Collection<? extends Object[]> c) {
		throw new UnsupportedOperationException(":) 还没实现");
	}

	public void clear() {
		list.clear();
		map.clear();
	}

	public boolean contains(Object key) {
		return map.containsKey(key);
	}

	public boolean containsAll(Collection<?> c) {
		throw new UnsupportedOperationException(":) 还没实现");
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public Iterator<Object[]> iterator() {
		return list.iterator();
	}

	public boolean remove(Object key) {
		if (map.containsKey(key)) {
			list.remove(((Integer) map.get(key)).intValue());
			map.remove(key);
		}
		return false;
	}

	public boolean removeAll(Collection<?> c) {
		throw new UnsupportedOperationException(":) 还没实现");
	}

	public boolean retainAll(Collection<?> c) {
		throw new UnsupportedOperationException(":) 还没实现");
	}

	public int size() {
		return list.size();
	}

	public Object[] toArray() {
		return list.toArray();
	}

	public <T> T[] toArray(T[] a) {
		throw new UnsupportedOperationException(":) 还没实现");
	}

	public static void main(String[] args) {
		MyListMap map = new MyListMap();
		map.put("1", "aaaaa");
		map.put("3", "bbbb");
		map.put("2", "cccc");

		for (Object[] a : map) {
			System.out.println(a[0] + "-->" + a[1]);
		}
	}
}
